﻿using System;
using System.Configuration;

namespace LaxCraft.Domain
{
	public class PlayerStat : EntityBase
	{
        public virtual Stat Stat { get; set; }
        public virtual double XP { get; set; }

        public virtual int GetLevel()
        {
            if (XP == 0) return 1;
            //Level=(ln(XP/FirstLevel))/ln(PercentGrowth)
            var firstLevel = int.Parse(ConfigurationManager.AppSettings["XPFirstLevel"]);
            var percentGrowth = double.Parse(ConfigurationManager.AppSettings["XPPercentGrowth"]);
            return (int) Math.Floor((Math.Log(XP / firstLevel)) / Math.Log(percentGrowth));
        }
        
        public virtual int GetLevelXP(int level)
        {
            if (level == 1)
                return 0;
            //XP=firstLevel*(growth^previousLevel)
            var firstLevel = int.Parse(ConfigurationManager.AppSettings["XPFirstLevel"]);
            var percentGrowth = double.Parse(ConfigurationManager.AppSettings["XPPercentGrowth"]);
            return (int)(firstLevel * Math.Pow(percentGrowth, (double)level));
        }

        public virtual int GetMinXP()
        {
            var level = GetLevel();

            return GetLevelXP(level);
        }

        public virtual int GetMaxXP()
        {
            var level = GetLevel() + 1;
            var result = GetLevelXP(level);
            return result;
        }

        public virtual int GetXPPercent()
        {
            var floor = GetMinXP();
            var ceiling = GetMaxXP();
            return (int)((XP - floor)*100)/(ceiling - floor);
        }

	}
}